/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import java.applet.Applet;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Jack
 */
public class ImageThread extends Thread {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Object waitLock = new Object();
    private boolean stop = false;
    private IUpdateImage updator;
    private String url;
    private final static String NAME = "ImageThread";

    public ImageThread() {
        setName(NAME);
    }

    /**
     *
     * @param update
     */
    public ImageThread(IUpdateImage update) {
        updator = update;
        setName(NAME);
    }
//    private static ImageThread instance;
//
//    public static ImageThread getInstance() {
//        if (instance == null) {
//            instance=new ImageThread();
//        }
//        return instance;
//    }

    @Override
    public void run() {
        log.info("{} started.", getName());
        do {
            log.info("waiting for signal...");
            waitForSignal();
            if (isStop()) {
                break;
            }
            updator.updateImage(getImageData());
            My12306App.getAudioThread().play();
            synchronized (this) {
                notifyAll();
            }
            log.info("waiting for signal... signal received");
        } while (!isStop());
        log.info("{} ended.", getName());
    }

    /**
     * get image byte[] data
     *
     * @return
     */
    private byte[] getImageData() {
        byte[] image = null;
        try {
            int count = 1;
            do {
                log.info("loading image {} time(s)", count);
                image = updator.getTrainClient().getRandCodeBytes(url);
            } while ((image == null || image.length == 0) && ++count <= 3);
        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage(), ex);
        } catch (Error ex) {
            log.error(ex.getLocalizedMessage(), ex);
        }
        return image;
    }

    public void continueRun() {
        synchronized (waitLock) {
            log.info("sending signal...");
            waitLock.notifyAll();
            log.info("sending signal... done");
        }
    }

    /**
     * send signal to continue run
     *
     * @param url
     */
    public void download(String url) {
        synchronized (waitLock) {
            this.url = url;
            log.info("sending signal...");
            waitLock.notifyAll();
            log.info("sending signal... done");
        }
    }

    /**
     * wait signal
     */
    public void waitForSignal() {
        synchronized (waitLock) {
            try {
                waitLock.wait();
            } catch (InterruptedException ie) {
                // continue
            }
        }
    }

    /**
     * set stop signal
     */
    public synchronized void setStop() {
        stop = true;
        continueRun();
    }

    /**
     * @return the stop
     */
    public synchronized boolean isStop() {
        return stop;
    }

    /**
     * @param updator the updator to set
     */
    public void setUpdator(IUpdateImage updator) {
        this.updator = updator;
//        synchronized (this) {
//            notifyAll();
//        }
    }
}
